import time
from tkinter import END, TOP, DISABLED, LEFT, IntVar, RIGHT, StringVar, Menu
from tkinter.scrolledtext import ScrolledText
from tkinter.messagebox import showerror
from tkinter.ttk import Button, Frame, LabelFrame, Label, Checkbutton
from taobao_check.tools import PATH


class BrushTab(Frame):
    def __init__(self, name):
        Frame.__init__(self)
        self.name = name
        self.msg = StringVar()
        self.show_debug = IntVar()
        self.log_path = PATH.log / self.name
        self.log_path.mkdir(parents=True, exist_ok=True)
        self.init_ui()

    def init_ui(self):
        # 按钮模块
        btn_frame = LabelFrame(self, height=400, text="选项")
        self.user_label = Label(btn_frame, text="【】", font=("微软雅黑", 10), foreground="green")
        self.user_label.pack(side=TOP, anchor='center', padx=20, pady=5)
        # 状态标签
        self.state_label = Label(btn_frame, text="未开始", font=("微软雅黑", 10), foreground="red")
        self.state_label.pack(side=TOP, anchor='center', padx=20, pady=10)
        # 准备环境
        self.ready_btn = Button(btn_frame, text="登录平台", cursor='hand2', width=10, state=DISABLED)
        # self.ready_btn.pack(side=TOP, anchor='center', padx=20, pady=10)
        # 开始按钮
        self.start_btn = Button(btn_frame, text="开始验号", cursor='hand2', width=10, state=DISABLED)
        self.start_btn.pack(side=TOP, anchor='center', padx=20, pady=10)
        # 结束按钮
        self.stop_btn = Button(btn_frame, text="停止验号", cursor='hand2', width=10, state=DISABLED)
        self.stop_btn.pack(side=TOP, anchor='center', padx=20, pady=10)
        # 布局打包
        btn_frame.pack(side=LEFT, anchor='n', pady=10)

        # 日志模块
        log_frame = LabelFrame(self, height=500, text='日志')
        # 日志文本框
        self.log_text = ScrolledText(log_frame, height=40, background='#ffffff')
        self.log_text.pack(side=TOP, anchor='n', padx=10, pady=10)
        self.log_text.config(state=DISABLED)
        # 清空日志的按钮
        log_btn = Button(log_frame, text="清空日志", cursor='hand2', command=self.clear_log)
        log_btn.pack(side=RIGHT, anchor='se', pady=5, padx=20)
        # 是否打印debug
        Checkbutton(log_frame, text="debug信息", variable=self.show_debug).pack(side=RIGHT, anchor='se', pady=5, padx=20)
        # 布局打包
        log_frame.pack(side=TOP, pady=10, fill='both')

        # menu = Menu(self, tearoff=0)
        # menu.add_command(label="复制", command=self.on_copy)

        # def popupmenu(event):
        #     menu.post(event.x_root, event.y_root)
        # # 绑定按钮
        # self.log_text.bind("<Button-3>", popupmenu)

        Label(self, font=("微软雅黑", 10), textvariable=self.msg, foreground="green", wraplength=560, justify="left").pack(side=LEFT, padx=5, pady=10)

        self.pack()

    # def on_copy(self):
    #     self.text = self.log_text.get(1.0, END)
    #     self.clipboard_append(self.text)

    def set_label(self, text, color):
        self.state_label.config(text=text, foreground=color)

    def set_msg(self, msg):
        self.msg.set(msg)

    def print_log(self, log, debug=False):
        """打印日志"""
        time_now = time.localtime(time.time())
        timestamp = time.strftime('%Y-%m-%d %H:%M:%S', time_now)
        if debug:
            log = f"{timestamp} (debug){log}\n"
        else:
            log = f"{timestamp}  {log}\n"
        log_file = self.log_path / time.strftime("log-%Y-%m-%d.txt", time_now)
        with open(log_file, "a+", encoding="utf-8") as f:
            f.write(log)

        if debug and not self.show_debug.get():
            return

        self.log_text.config(state='normal')
        self.log_text.insert(END, log)
        self.log_text.see(END)
        self.log_text.config(state='disabled')

    def clear_log(self):
        """清空日志"""
        self.log_text.config(state='normal')
        self.log_text.delete(1.0, END)
        self.log_text.config(state='disabled')

    def start(self):
        showerror(title='错误', message='请等待验号环境准备就绪!')

    def stop(self):
        showerror(title='错误', message='请等待验号环境准备就绪!')
